﻿using System.Windows.Forms;
using Engine.Objects.StaticObjects;
using Microsoft.Xna.Framework;
using ThinkDotNetEditorLib.Commands;
using ThinkDotNetEditorLib.Commands.Arena;
using ThinkDotNetEditorLib.Models;
using ThinkDotNetEditorLib.Views;

namespace ThinkDotNetEditorLib.Controllers
{  
    public class ArenaController:ControllerBase,IArenaController
    {
        private readonly IStaticObjectController _staticObjectController;
        private readonly IArenaView _arenaView;
        
        public ArenaController(IStaticObjectController staticObjectController, IArenaView arenaView)
        {
            _staticObjectController = staticObjectController;
            _arenaView = arenaView;
            _arenaView.MouseDown += ArenaViewMouseDown;
            ServiceLocator.Instance.RegisterInstance(typeof (IArenaController), this);
        }
        #region Properties

        private Arena _arena;
        public Arena Arena
        {
            get
            {
                if (_arena != null) return _arena;
                _arena = ServiceLocator.Instance.Resolve<Arena>();
                return _arena;
            }
        }

        public StaticObject SelectedObject { get; set; }

        #endregion

        #region Events

        private void ArenaViewMouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (_staticObjectController.SelectedStaticObjectInfo != null)
                    AddNewStaticObject(new Point(e.X, e.Y));
                else
                    SelectedObject = Arena.SelectObject(new Point(e.X, e.Y));
            }
            else if (e.Button == MouseButtons.Right)
                Arena.UnselectAll();
        }       

        #endregion   
        
        #region Public Methods

        public void RemoveSelectedObjects()
        {
            if (SelectedObject != null)
            {
                RemoveStaticObjectCmd command = new RemoveStaticObjectCmd(SelectedObject);
                CommandManager.Instance.Execute(command);
                _staticObjectController.SelectedStaticObjectInfo = null;
                _arenaView.Shell.RefreshUndoRedoIsEnabled();
            }
        }
        
        #endregion

        #region Private Methods

        private void AddNewStaticObject(Point point)
        {
            Vector3? worldPosition = Arena.IntersectCursor(point);
            if (worldPosition != null)
            {
                AddStaticObjectCmd command = new AddStaticObjectCmd(worldPosition.Value,
                                                                    _staticObjectController.
                                                                        SelectedStaticObjectInfo);
                CommandManager.Instance.Execute(command);
                _staticObjectController.SelectedStaticObjectInfo = null;
                _arenaView.Shell.RefreshUndoRedoIsEnabled();
            }
        }
        #endregion
    }
}
